import re


quicks = {}


def build_quick():
    f = open("./quick5.dict.yaml", "r")
    lines = f.readlines()

    pattern = re.compile(r"^[x|z].*")

    for line in lines:
        items = line.rstrip().split("\t")
        character = items[0]
        cangjie = items[1]
        if pattern.match(cangjie):
            continue
        quick = re.sub(r"^([^z])\w+(\w)$", r"\1\2", cangjie)
        if quicks.get(character):
            print()
        quicks[character] = quick


def to_fly(s):
    # - erase/^xx$/
    # - derive/^([jqxy])u$/$1v/
    # - derive/^([aoe])([ioun])$/$1$1$2/
    ss = s
    # - xform/^([aoe])(ng)?$/$1$1$2/
    ss = re.sub(r"^([aoe])(ng)?$", r"\1\1\2", ss)
    # - xform/iu$/Q/
    ss = re.sub(r"iu$", "Q", ss)
    # - xform/(.)ei$/$1W/
    ss = re.sub(r"(.)ei$", r"\1W", ss)
    # - xform/uan$/R/
    ss = re.sub(r"uan$", "R", ss)
    # - xform/[uv]e$/T/
    ss = re.sub(r"[uv]e$", "T", ss)
    # - xform/un$/Y/
    ss = re.sub(r"un$", "Y", ss)
    # - xform/^sh/U/
    ss = re.sub(r"^sh", "U", ss)
    # - xform/^ch/I/
    ss = re.sub(r"^ch", "I", ss)
    # - xform/^zh/V/
    ss = re.sub(r"^zh", "V", ss)
    # - xform/uo$/O/
    ss = re.sub(r"uo$", "O", ss)
    # - xform/ie$/P/
    ss = re.sub(r"ie$", "P", ss)
    # - xform/i?ong$/S/
    ss = re.sub(r"i?ong$", "S", ss)
    # - xform/ing$|uai$/K/ss
    ss = re.sub(r"ing$|uai$", "K", ss)
    # - xform/(.)ai$/$1D/
    ss = re.sub(r"(.)ai$", r"\1D", ss)
    # - xform/(.)en$/$1F/
    ss = re.sub(r"(.)en$", r"\1F", ss)
    # - xform/(.)eng$/$1G/
    ss = re.sub(r"(.)eng$", r"\1G", ss)
    # - xform/[iu]ang$/L/
    ss = re.sub(r"[iu]ang$", "L", ss)
    # - xform/(.)ang$/$1H/
    ss = re.sub(r"(.)ang$", r"\1H", ss)
    # - xform/ian$/M/
    ss = re.sub(r"ian$", "M", ss)
    # - xform/(.)an$/$1J/
    ss = re.sub(r"(.)an$", r"\1J", ss)
    # - xform/(.)ou$/$1Z/
    ss = re.sub(r"(.)ou$", r"\1Z", ss)
    # - xform/[iu]a$/X/
    ss = re.sub(r"[iu]a$", "X", ss)
    # - xform/iao$/N/
    ss = re.sub(r"iao$", "N", ss)
    # - xform/(.)ao$/$1C/
    ss = re.sub(r"(.)ao$", r"\1C", ss)
    # - xform/ui$/V/
    ss = re.sub(r"ui$", "V", ss)
    # - xform/in$/B/
    ss = re.sub(r"in$", "B", ss)
    # - xlit/QWRTYUIOPSDFGHJKLZXCVBNM/qwrtyuiopsdfghjklzxcvbnm/
    ss = ss.lower()
    # print(s, ss)
    return ss


def test_flypy():
    test_cases = [
        "qiu",
        "wei",
        "ruan",
        "yue",
        "yun",
        "shu",
        "chi",
        "song",
        "yong",
        "dai",
        "fen",
        "geng",
        "hang",
        "an",
        "kuai",
        "ying",
        "liang",
        "wang",
        "zou",
        "xia",
        "wa",
        "pie",
        "cao",
        "zhui",
        "yu",
        "bin",
        "niao",
        "mian",
        "ai",
        "ao",
        "en",
        "yin",
        "a",
        "o",
        "e",
    ]
    for case in test_cases:
        flypy = to_fly(case)
        print(case, flypy)


# test_flypy()

# exit(0)


def build_characters():
    f = open("./luna_pinyin.dict.yaml", "r")
    lines = f.readlines()
    for line in lines:
        items = line.rstrip().split("\t")
        character = items[0]
        pinyin = items[1]
        weight = ""
        if len(items) > 2:
            weight = items[2]
        flypy = to_fly(pinyin)
        try:
            quick = quicks[character]
            print("{}\t{}\t{}".format(character, flypy + quick, weight))
        except KeyError:
            continue


def build_words(filename):
    f = open(filename, "r")
    lines = f.readlines()
    p_pinyin = re.compile(r"^[a-z]")
    for line in lines:
        if line.startswith("#") or len(line.strip()) < 1:
            continue
        items = line.rstrip().split("\t")
        flys = ""
        weight = ""

        length = len(items)

        if length == 1:
            print(items[0])
        elif length == 2:
            words = items[0]
            if p_pinyin.match(items[1]):
                pinyins = items[1]
                flypys = [to_fly(s) for s in pinyins.split(" ")]
                flys = " ".join(flypys)
                print("{}\t{}".format(words, flys))
            else:
                weight = items[1]
                print("{}\t{}".format(words, weight))
        else:
            words = items[0]
            pinyins = items[1]
            flypys = [to_fly(s) for s in pinyins.split(" ")]
            flys = " ".join(flypys)
            print("{}\t{}\t{}".format(words, flys, weight))


build_quick()
build_characters()
build_words("./base.dict.yaml")
build_words("./ext.dict.yaml")
build_words("./tencent.dict.yaml")
build_words("./others.dict.yaml")
